<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="searchHandle">
      <el-row>
        <el-form-item label="销售类型">
          <el-select v-model="dataForm.saleType">
            <el-option v-for="item in saleTypeData" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
          <!-- <el-input v-model="dataForm.id" placeholder="请输入商品类别ID"></el-input> -->
        </el-form-item>
        <el-form-item label="销售单号">
          <el-input v-model="dataForm.billId"></el-input>
        </el-form-item>
        <el-form-item label="顾客姓名">
          <el-input v-model="dataForm.customerName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
          <el-button icon="el-icon-delete" @click="clearFormData">清空</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <div class="avue-crud">
      <el-table :data="dataList" height="calc(100vh - 215px)" border v-loading="dataListLoading">
        <el-table-column label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <!-- <el-button type="text" size="small" icon="el-icon-edit" @click="checkDetail(scope.row.id)">详情</el-button> -->
            <el-button type="text" icon="el-icon-edit" @click="paymentHandle(scope.row)">补交</el-button>
            <!-- <el-button v-if="permissions.eims_sale_del" type="text" size="small" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button> -->
          </template>
        </el-table-column>
        <el-table-column prop="billId" label="销售单号" width="160">
          <template slot-scope="scope">
            <el-button type="text" @click="checkDetailByOrder(scope.row.billId)">{{scope.row.billId}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="customerName" label="顾客姓名" width="100"></el-table-column>
        <el-table-column prop="salerUserName" label="销售人员" width="100"></el-table-column>
        <el-table-column prop="saleType" label="销售类型" width="100">
          <template slot-scope="scope">
            {{getNameByValue(scope.row.saleType)}}
          </template>
        </el-table-column>
        <el-table-column prop="originalPrice" label="原价合计"></el-table-column>
        <el-table-column prop="discountPrice" label="折扣金额"></el-table-column>
        <el-table-column prop="removePrice" label="抹零金额"></el-table-column>
        <el-table-column prop="receivablePrice" label="应收金额"></el-table-column>
        <el-table-column prop="paidPrice" label="实缴金额"></el-table-column>
        <el-table-column prop="arrears" label="欠款金额"></el-table-column>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>

    <!-- 弹窗，  结账 -->
    <payment v-if="paymentVisible" ref='payment' :saleDataList="dataList" @refreshDataList="getDataList"></payment>
    <!-- 弹窗，配镜单详情 -->
    <order-info v-if="infoVisible" ref="infoDialog"></order-info>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { remote } from '@/api/admin/dict'
  import { getCtgList } from '@/api/eims/ctg'
  import {
    getArrearsSaleInfo,
    delMatchingObj
  } from '@/api/eims/cashier/payment'
  import { getSaleList } from '@/api/eims/sale'

  import Payment from './Payment'
  import OrderInfo from '@/views/eims/optician/glassesinfo/detail'

  export default {
    data() {
      return {
        printUrl: '',
        dataForm: {
          saleType: '',
          billId: '',
          customerName: ''
        },
        ctgOption: '',
        dataList: [],
        saleTypeData: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        paymentVisible: false,
        infoVisible: false
      }
    },
    created() {
      this.getDataList()
      remote('sale_type').then(result => {
        this.saleTypeData = result.data.data
      })
    },
    computed: {
      ...mapGetters(['permissions', 'userInfo', 'bizData'])
    },
    components: {
      Payment,
      OrderInfo
    },
    methods: {
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      //  清空搜索表单
      clearFormData() {
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.dataForm.deptId = this.userInfo.lessDeptId
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        }
        let params2 = this.filterParams(this.dataForm)
        getArrearsSaleInfo(Object.assign(params1, params2)).then(response => {
          if (response.data.code == '0') {
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          } else {
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 结账
      paymentHandle(data) {
        this.paymentVisible = true
        this.$nextTick(() => {
          this.$refs.payment.init(data)
        })
      },
      //  查看配镜单详情
      checkDetailByOrder(order) {
        this.infoVisible = true;
        this.$nextTick(() => {
          this.$refs.infoDialog.init('', order, '配镜单详情');
        });
      },
      //  根据value值获取name
      getNameByValue(value) {
        let obj = {}
        obj = this.saleTypeData.find(item => {
          return item.value == value
        })
        if (obj) {
          return obj.label
        }
      },
      //  获取商品类别列表
      getGoodsCtgList() {
        getCtgList().then(response => {
          if (response.data.code == '0') {
            this.ctgOption = response.data.data
          } else {
            this.$message.error(response.data.msg)
          }
        })
      },
      //  打印配镜单
      saleGlassPrint(id) {
        let params = '/saleGlassPrint_ykyy.cpt&wFlag=1&saleId='
        this.printUrl = this.bizData.printUrl + params + id
      },
      //  打印退款单
      refundFormPrint(id) {
        let params = '/saleGlassPrint_ykyy.cpt&wFlag=-1&saleId='
        this.printUrl = this.bizData.printUrl + params + id
      }
    }
  }
</script>
